home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 February / EnigmA AMIGA RUN 15 (1997)(G.R. Edizioni)(IT)[!][issue 1997-02][PLANET CD V].iso / enigma / earcd / emula / arosdv19.lha / AROS / exec / obtainsemaphoreshared.c < prev    next >
C/C++ Source or Header  |  1996-10-24  |  3KB  |  113 lines

  1. /*
  2.     (C) 1995-96 AROS - The Amiga Replacement OS
  3.     $Id: obtainsemaphoreshared.c,v 1.5 1996/10/24 15:50:52 aros Exp $
  4.     $Log: obtainsemaphoreshared.c,v $
  5.     Revision 1.5  1996/10/24 15:50:52  aros
  6.     Use the official AROS macros over the __AROS versions.
  7.  
  8.     Revision 1.4  1996/08/13 13:56:04  digulla
  9.     Replaced AROS_LA by AROS_LHA
  10.     Replaced some AROS_LH*I by AROS_LH*
  11.     Sorted and added includes
  12.  
  13.     Revision 1.3  1996/08/01 17:41:14  digulla
  14.     Added standard header for all files
  15.  
  16.     Desc:
  17.     Lang: english
  18. */
  19. #include "exec_intern.h"
  20. #include "semaphores.h"
  21.  
  22. /*****************************************************************************
  23.  
  24.     NAME */
  25.     #include <exec/semaphores.h>
  26.     #include <clib/exec_protos.h>
  27.  
  28.     AROS_LH1(void, ObtainSemaphoreShared,
  29.  
  30. /*  SYNOPSIS */
  31.     AROS_LHA(struct SignalSemaphore *, sigSem, A0),
  32.  
  33. /*  LOCATION */
  34.     struct ExecBase *, SysBase, 113, Exec)
  35.  
  36. /*  FUNCTION
  37.     Get a shared lock on a semaphore. If the lock cannot be obtained
  38.     immediately this function waits. There may be more than one shared
  39.     locks at the same time but only one exclusive one. An exclusive
  40.     lock prevents shared locks. Shared locks are released with
  41.     ReleaseSemaphore().
  42.  
  43.     INPUTS
  44.     sigSem - Pointer to semaphore structure
  45.  
  46.     RESULT
  47.  
  48.     NOTES
  49.     This function preserves all registers.
  50.  
  51.     EXAMPLE
  52.  
  53.     BUGS
  54.  
  55.     SEE ALSO
  56.     ReleaseSemaphore()
  57.  
  58.     INTERNALS
  59.  
  60.     HISTORY
  61.     29-10-95    digulla automatically created from
  62.                 exec_lib.fd and clib/exec_protos.h
  63.     21-01-96    fleischer implementation
  64.  
  65. *****************************************************************************/
  66. {
  67.     AROS_LIBFUNC_INIT
  68.     AROS_LIBBASE_EXT_DECL(struct ExecBase *,SysBase)
  69.     struct Task *me;
  70.  
  71.     /* Get pointer to current task */
  72.     me=SysBase->ThisTask;
  73.  
  74.     /* Arbitrate for the semaphore structure */
  75.     Forbid();
  76.  
  77.     /* Check if there's an exclusive lock on the semaphore */
  78.     if(sigSem->ss_NestCount>0)
  79.     {
  80.     /* Yes. Is it owned by the current task? */
  81.     if(sigSem->ss_Owner!=me)
  82.     {
  83.         /* No. Prepare a node for the waiting queue. */
  84.         struct SemaphoreNode sn;
  85.         sn.node.ln_Pri =SN_TYPE_OBTAIN;
  86.         sn.node.ln_Name=(char *)SM_SHARED;
  87.         sn.task       =me;
  88.  
  89.         /* Add it. */
  90.         AddTail((struct List *)&sigSem->ss_WaitQueue,&sn.node);
  91.  
  92.         /* Wait until the semaphore is free */
  93.         Wait(SEMAPHORESIGF);
  94.  
  95.         /* ss_NestCount and ss_Owner are already set by ReleaseSemaphore() */
  96.     }else
  97.         /* Add one nesting level more */
  98.         sigSem->ss_NestCount++;
  99.     }else
  100.     {
  101.     /* There's no exclusive lock on the semaphore. Get a shared one. */
  102.     sigSem->ss_NestCount--;
  103.  
  104.     /* Invalidate the owner field - so no task may think this semaphore is his. */
  105.     sigSem->ss_Owner=NULL;
  106.     }
  107.  
  108.     /* All done. */
  109.     Permit();
  110.     AROS_LIBFUNC_EXIT
  111. } /* ObtainSemaphoreShared */
  112.  
  113.